﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AppManagement.BusManagers
{
    #region Using
    using DevComponents.DotNetBar;
    using AppManagement.Repositories;
    using AppManagement.Helpers;
    #endregion

    public partial class FrmChonDV : Office2007Form
    {
        private string _selectedDV;
        //  event handler to transfer data to other forms
        public EventHandler Handler { get; set; }

        public FrmChonDV()
        {
            InitializeComponent();
        }

        private void FrmChonDV_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        public void LoadData()
        {
            treeView1.Nodes.Clear();
            TreeNode root = new TreeNode("Danh mục các dịch vụ");
            root.ImageIndex = 0;
            treeView1.Nodes.Add(root);

            try
            {
                // Load list all of nhom dich vu
                var lstNhomDV = NhomDichVuRepository.SelectAll();
                for (int i = 0; i < lstNhomDV.Count; i++)
                {
                    var NhomDV = lstNhomDV[i];
                    TreeNode NhomDVnode = new TreeNode(NhomDV.MaNhomDV + " - " + NhomDV.TenNhomDV);
                    NhomDVnode.ImageIndex = 1;
                    root.Nodes.Add(NhomDVnode);

                    // Load giai doan corresponding with specified cong trinh
                    var lstDV = DichVuRepository.SelectByMaNhomDV(NhomDV.MaNhomDV);
                    for (int j = 0; j < lstDV.Count; j++)
                    {
                        var DV = lstDV[j];
                        TreeNode DVnode = new TreeNode(DV.MaDV + " - " + DV.TenDV);
                        DVnode.ImageIndex = 2;
                        NhomDVnode.Nodes.Add(DVnode);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }

        private void btChon_Click(object sender, EventArgs e)
        {
            var DV = DichVuRepository.SelectByID(_selectedDV);
            TransferDataInfo(this, new TransferSimpleDataEvent(_selectedDV + "#" + DV.TenDV));
        }

        public int GetLevelTreeView(TreeNode node)
        {
            if (node.Parent == null)
                return 1;
            else if (node.Parent.Parent == null)
                return 2;
            else if (node.Parent.Parent.Parent == null)
                return 3;
            else if (node.Parent.Parent.Parent.Parent == null)
                return 4;
            return -1;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            int level = GetLevelTreeView(treeView1.SelectedNode);
            if (level == 3)
            {
                btChon.Enabled = true;
                string maDV = treeView1.SelectedNode.Text.Split('-')[0].Trim();
                var DV = DichVuRepository.SelectByID(maDV.Trim());
                if (DV != null)
                {
                    _selectedDV = DV.MaDV;
                }
            }
            else
            {
                btChon.Enabled = false;
            }
        }

        /// <summary>
        ///  function is used to transfer data when event would be raised
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void TransferDataInfo(object sender, TransferSimpleDataEvent e)
        {
            this.Close();
            this.Handler(this, e);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FrmChonDV_Shown(object sender, EventArgs e)
        {
        }
    }
}